home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / macros / latex209 / contrib / psfrag / ps2frag.ps < prev    next >
Text File  |  1993-10-30  |  5KB  |  183 lines

  1. %! PS-Adobe-2.0
  2. %
  3. % PostScript program for reading an EPSF file and writing out a LaTeX
  4. % file that contains information about each piece of text shown by the
  5. % ps file.
  6. % GhostScript Usage:
  7. %
  8. %    dx dy (EPSFbaseName.ext) FragConvert
  9. %
  10. %    Reads EPSFbaseName.ext and writes latex information to EPSFbaseName.frag
  11. %    The show text coordinates are offset by dx, dy.
  12. %
  13. % Revision History:
  14. %   30-Oct-93   Piet Tutelaers  added /FragFile function and renamed it
  15. %                               to ps2frag.ps (instead of ps2psfrag.ps)
  16. %   20-Sep-92   Craig Barratt    Released version 1.1.
  17. %   13-Sep-92   Craig Barratt    Added support for ashow, widthshow, awidthshow,
  18. %                and kshow.
  19. %   21-Jul-92   Craig Barratt    Correctly handle empty show strings.  Added
  20. %    9-Jun-92   Craig Barratt    Changed egrep to grep for system V users.
  21. %    1-Jun-92   Craig Barratt    Released version 1.0.
  22. %   23-Feb-92   Craig Barratt    Initial version.
  23. %
  24. % Send comments and bugs to Craig Barratt (craig@isl.stanford.edu)
  25. %
  26.  
  27. /fragdict 30 dict def
  28.  
  29. fragdict begin
  30.  
  31. % write a string to the output file
  32. /w { FragOutFile exch writestring } bind def
  33. % write an argument to the output file
  34. /warg { ({) w w (}) w } bind def
  35. /p { 16 string cvs print flush } def
  36. /sp { ( ) p } def
  37.  
  38. %
  39. % writes out latex information into the psfrag file for
  40. % each show operator
  41. %
  42. /FragProcessShow {
  43.     FragPass 1 eq
  44.     {
  45.     % pass 1: write out just the show string
  46.     /s exch def
  47.     % we only care about non-empty show strings
  48.     s () eq not
  49.     {
  50.         s (\\tex) anchorsearch
  51.         {
  52.         pop pop
  53.         }
  54.         {
  55.         pop
  56.         (    \\PsFragShowString) w s warg (\n) w
  57.         } ifelse
  58.     } if
  59.     }
  60.     {
  61.     % pass 2: write out psfrag info
  62.     /s exch def
  63.     % we only care about non-empty show strings
  64.     s () eq not
  65.     {
  66.         gsave
  67.         matrix currentmatrix /origMatrix exch def
  68.         currentpoint /origy exch def /origx exch def
  69.         % write out the string as the first arg
  70.         s (\\tex) anchorsearch {
  71.         % define this fragment using an empty label
  72.         pop (    \\psfrag{}) w w (\n) w
  73.         % now immediately refer to the empty label
  74.         (    \\PsFragInfo{}) w
  75.         }
  76.         {
  77.         pop (    \\PsFragInfo) w s warg
  78.         } ifelse
  79.         % write out the current point in abs coords as the next two args
  80.         initmatrix currentpoint /abslly exch def /absllx exch def
  81.         absllx FragDx add 30 string cvs warg
  82.         abslly FragDy add 30 string cvs warg
  83.         % get width, height and orientation of the string
  84.         origMatrix setmatrix
  85.         newpath 0 0 moveto s true charpath pathbbox
  86.         /ury exch def /urx exch def pop pop
  87.         origx origy moveto urx 0 rmoveto
  88.         initmatrix currentpoint /abslry exch def /abslrx exch def
  89.         origMatrix setmatrix
  90.         origx origy moveto 0 ury rmoveto
  91.         initmatrix currentpoint /absuly exch def /absulx exch def
  92.  
  93.         % write out dx and dy for the lower left to lower right vector
  94.         abslrx absllx sub 30 string cvs warg
  95.         abslry abslly sub 30 string cvs warg
  96.  
  97.         % write out dx and dy for the lower left to upper left vector
  98.         absulx absllx sub 30 string cvs warg
  99.         absuly abslly sub 30 string cvs warg
  100.  
  101.         % write out the text angle (keep it between -180 and 180,
  102.         % and round to the nearest 0.01)
  103.         abslry abslly sub abslrx absllx sub
  104.         % avoid atan(0,0): check if both numbers are zero
  105.         % (bug reported by Noel Doughty nad@phys.canterbury.ac.nz)
  106.         2 copy abs exch abs add
  107.         0 eq { pop pop 0 } { atan } ifelse
  108.         dup 360 div round 360 mul sub
  109.         100 mul round 100 div 30 string cvs warg
  110.  
  111.         % finish up
  112.         (\n) w
  113.         origMatrix setmatrix
  114.         origx origy moveto
  115.         grestore
  116.     } if
  117.     } ifelse
  118. } bind def
  119.  
  120. end
  121.  
  122. userdict begin
  123.  
  124. %
  125. % redefine all the show operators to call FragProcessShow,
  126. % and then do the normal show
  127. %
  128. /widthshow  { dup fragdict begin FragProcessShow end widthshow  } bind def
  129. /awidthshow { dup fragdict begin FragProcessShow end awidthshow } bind def
  130. /ashow      { dup fragdict begin FragProcessShow end ashow      } bind def
  131. /kshow      { dup fragdict begin FragProcessShow end kshow      } bind def
  132. /show       { dup fragdict begin FragProcessShow end show       } bind def
  133.  
  134. % Added for portability reasons (rcpt@urc.tue.nl: 931027)
  135. /FragFile { % filename[.ext] FragFile filename.frag
  136.   /fn exch def
  137.   fn length /len exch def
  138.   /i 0 def
  139.   { fn i get
  140.     46 eq { exit } if
  141.     i 1 add dup /i exch def
  142.     len eq { exit } if
  143.   } loop
  144.   i 5 add string /ff exch def
  145.   ff 0 fn 0 i getinterval putinterval
  146.   ff i (.frag) putinterval
  147.   ff
  148. } bind def
  149.  
  150. /FragConvert {
  151.     fragdict begin
  152.     /FragInFileName exch def
  153.     FragInFileName FragFile (w) file /FragOutFile exch def
  154.     /FragDy exch def
  155.     /FragDx exch def
  156.     (\%\n\% File automatically generated by ps2psfrag and GhostScript.\n) w
  157.     (\% It is included by \\epsfbox or \\psfragspecial.\n) w
  158.     (\% Do not \\input directly, or edit!\n\%\n) w
  159.     (\\begin{PSFragmentLocs}\n) w
  160.     %
  161.     % I'm really lazy, so we run through the ps file twice.
  162.     % The first pass generates a list of \PsFragShowString{} calls.
  163.     % These are used by latex to check if the strings is being replaced.
  164.     % The second pass generates a list of \PsFragInfo{} calls, which
  165.     % contains complete information about each ps fragment.
  166.     %
  167.     /FragPass 1 def
  168.     gsave /FragVM save def
  169.         FragInFileName userdict begin run end
  170.     FragVM restore grestore
  171.     (    \\put(0,0){\\special{\\PsFragSpecialArgs}}\n) w
  172.     /FragPass 2 def
  173.     gsave /FragVM save def
  174.         FragInFileName userdict begin run end
  175.     FragVM restore grestore
  176.     (\\end{PSFragmentLocs}\n) w
  177.     FragOutFile closefile
  178.     end
  179. } bind def
  180.  
  181. end
  182.